
/**
 * AppleIIGo
 * Character Set Loader class
 * (C) 2008 by Marc S. Ressl (mressl@umich.edu)
 * Released under the GPL
 */

#include "Motherboard.h"

#ifndef CHARACTERSETLOADER_H
#define CHARACTERSETLOADER_H

typedef enum {
	CHARACTERSETLANGUAGE_DEFAULT,
	CHARACTERSETLANGUAGE_UK,
	CHARACTERSETLANGUAGE_FRENCH,
	CHARACTERSETLANGUAGE_DANISH,
	CHARACTERSETLANGUAGE_SPANISH,
	CHARACTERSETLANGUAGE_ITALIAN,
	CHARACTERSETLANGUAGE_GERMAN,
	CHARACTERSETLANGUAGE_SWEDISH,
	CHARACTERSETLANGUAGE_END,
};

typedef enum {
	CHARACTERSETTYPE_II,
	CHARACTERSETTYPE_IIJPLUS,
	CHARACTERSETTYPE_IIE,
};

#define CHARACTERSET_CHARNUM		256
#define CHARACTERSET_BYTENUM		(CHARACTERSET_CHARNUM * 8)

#define CHARACTERSETMAP_FLASH		(1 << 8)
#define CHARACTERSETMAP_ALT			(1 << 9)
#define CHARACTERSETMAP_END			(1 << 10)

class CharacterSetLoader {
public:
	CharacterSetLoader(UINT32 videoType, class Resources * resources);
	
	bool loadCharacterSet(UINT32 characterSetType, char * characterSetRomImage);
	
	void setLanguage(UINT32 languageIndex);
	
	UINT8 ** getCharacterSetMap();
	
private:
	bool isIIeEnhanced;
	class Resources * resources;
	
	UINT32 characterSetType;
	UINT8 characterSet[CHARACTERSET_BYTENUM];
	UINT8 expandedCharacterSet[2 * CHARACTERSET_BYTENUM];
	UINT8 * characterSetMap[CHARACTERSETMAP_END];
	
	UINT32 languageIndex;
	
	void initLanguage();
	void initCharacterSetMap();
	void initCharacterSet();
	
	UINT32 reverseBits(UINT32 value, UINT32 bitNum);
	void convertCharacterSet(UINT8 * convertedCharacterSet,
							 UINT8 * characterSet,
							 UINT32 characterSetType);
	
	void copyCharacterSetChar(UINT8 * localizedCharacterSet,
							  UINT32 characterSetIndex,
							  UINT8 * characterSetChar,
							  bool isInverted);
	void localizeCharacter(UINT8 * localizedCharacterSet,
						   UINT32 asciiIndex,
						   UINT8 * characterSetChar);
	void localizeCharacterSet(UINT8 * localizedCharacterSet,
							  UINT8 * characterSet,
							  UINT32 characterSetType,
							  UINT32 languageIndex);
	
	void expandCharacterSet(UINT8 * expandedCharacterSet,
							UINT8 * characterSet);
	
	void updateCharacterSet();
};

#endif
